package com.bmps.springcloud.insuredmicroservice.service.trial.strategy;

import com.bmps.springcloud.common.utils.DateUtil;
import com.bmps.springcloud.insuredmicroservice.constant.accept.AcceptConstant;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.AppntModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.ProductModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.RequestModel;
import com.bmps.springcloud.insuredmicroservice.model.trail.TrialPersonModel;
import com.bmps.springcloud.insuredmicroservice.service.RedisService;
import com.bmps.springcloud.insuredmicroservice.util.trial.TrialUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author : 追到乌云的尽头找太阳-（Jacob）
 * @date : 2019/12/16 16:47
 **/
public class Hct019TrialStrategy implements TrialStrategy {

    private static Logger logger = LoggerFactory.getLogger(Hct019TrialStrategy.class);
    
    @Override
    public TrialPersonModel premiumTrialCalculationPrepare(ProductModel productModel, RequestModel requestModel, RedisService redisService) {
        
        String riskCode = productModel.getProdCode();
        TrialPersonModel trialPersonModel = new TrialPersonModel();
        trialPersonModel.setRiskCode(riskCode);
        trialPersonModel.setGet(productModel.getAmnt());
        AppntModel appntModel = requestModel.getCustInfo().getAppnt();
        trialPersonModel.setAppAge(DateUtil.getYears(appntModel.getAppntBirthday(), requestModel.getPolicyInfo().getPolApplyDate()));
        trialPersonModel.setSex(appntModel.getAppntSex());
        trialPersonModel.setInsuYearFlag(productModel.getInsureYearFlag());
        trialPersonModel.setPayEndYear(productModel.getPayYear());
        trialPersonModel.setInsuYear(productModel.getInsureYear());
       
        String payIntv = requestModel.getProductInfo().getPayIntv();
        trialPersonModel.setPayIntv(payIntv);
        if (StringUtils.equals("12",payIntv)){
            trialPersonModel.setPayIntvRate("1");
        }
        else if (StringUtils.equals("1", payIntv)) {
            trialPersonModel.setPayIntvRate("0.09");
        }
        else if (StringUtils.equals("3", payIntv)) {
            trialPersonModel.setPayIntvRate("0.262");
        }
        else if (StringUtils.equals("6", payIntv)) {
            trialPersonModel.setPayIntvRate("0.52");
        }else {
            trialPersonModel.setPayIntvRate("0");
        }

        StringBuilder sb = new StringBuilder(16);
        sb.append(AcceptConstant.REDIS_PREFIX_R004).append("|").append(riskCode);
        Object object = redisService.get(sb.toString());
        if (object == null) {
            return new TrialPersonModel();
        }
        TrialUtil.setSpecialElement(trialPersonModel, object.toString());
        logger.info("【" + productModel.getProdCode() + "】【VPU】【KEY】" + sb.toString());
        
        sb.setLength(0);
        sb.append(AcceptConstant.REDIS_PREFIX_R006).append("|").append(trialPersonModel.getRiskCode()).append("|G");
        object = redisService.get(sb.toString());
        if (object == null) {
            return new TrialPersonModel();
        }
        trialPersonModel.setFormula(object.toString());
        logger.info("【" + productModel.getProdCode() + "】【公式】【KEY】" + sb.toString());
        
        sb.setLength(0);
        sb.append(AcceptConstant.REDIS_PREFIX_R001).append("|").append(riskCode);
        object = redisService.get(sb.toString());
        if (object == null) {
            return new TrialPersonModel();
        }
        String result = TrialUtil.getRiskRateKey(trialPersonModel, object.toString());
        logger.info("【" + riskCode + "】【产品计算要素配置】【KEY】" + sb.toString());
        object = redisService.get(result);
        if (object == null) {
            return new TrialPersonModel();
        }
        TrialUtil.setElementRate(trialPersonModel, object.toString());
        logger.info("【产品计算要素配置】【KEY】" + sb.toString());
        sb.setLength(0);
        sb.append(AcceptConstant.REDIS_PREFIX_R005).append("|").append(riskCode).append("|G");
        object = redisService.get(sb.toString());
        if (object == null) {
            return new TrialPersonModel();
        }
        logger.info("【" + riskCode + "】【产品计算公式要素配置】【KEY】" + sb.toString());
        TrialUtil.setElementMapping(trialPersonModel, object.toString());
        return trialPersonModel;
       
    }
}
